package com.tuanzi.loan.web.bean.family;


import com.base.common.log.LogService;
import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.family.LoanChannelNdInfo;
import com.tuanzi.loan.business.entity.family.LoanLargeAmountPartnerControl;
import com.tuanzi.loan.business.service.family.LoanLargeAmountPartnerControlService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.vo.request.family.LoanLargeAmountPartnerControlSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanLargeAmountPartnerControlSearchResponse;
import com.tuanzi.loan.web.configuration.JSFListingBean;
import com.tuanzi.loan.web.lazy.family.LoanLargeAmountPartnerControlLazyDataModel;
import com.tuanzi.server.loan_base.model.db.LoanWholeSaleComment;
import com.tuanzi.server.loan_base.util.DateUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanLargeAmountPartnerControlBean
        extends JSFListingBean<LoanLargeAmountPartnerControlSearchRequest,LoanLargeAmountPartnerControlSearchResponse> {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:MM:ss");

    @Autowired
    LoanLargeAmountPartnerControlService service;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public List<String> channelList;

    @PostConstruct
    public void init(){
        initLazyable();
    }

    private void initLazyable() {
        $ = new LoanLargeAmountPartnerControlSearchRequest();
        lazyDataModel =  new LoanLargeAmountPartnerControlLazyDataModel($);
    }

    /**
     * 增加数据初始化
     */
    public void onCreateTarget(){
        $ = new LoanLargeAmountPartnerControlSearchRequest();
        channelList = Lists.newArrayList();
        $.setCreator(CurrentUser.get().getName());
        try {
            $.setCreateTime(sdf.parse(DateUtils.getCurrentTime()));
            $.setIscommit("是");
            $.setIsapprove("是");
            $.setIssuccess("是");
            $.setIsDistinct("是");
            $.setCfrom(service.judgeCfrom($.getPartnertype()));
            $.setUpdator(CurrentUser.get().getName());
            $.setUpdateTime(sdf.parse(DateUtils.getCurrentTime()));
            $.setChannel_n(null);
        } catch (Exception e) {
            LogService.error("日期转换异常",e);
            addErrorMessage("系统异常，请联系管理员");
        }
    }

    /**
     * 编辑数据初始化
     * @param response
     */
    public void onEditTarget(LoanLargeAmountPartnerControlSearchResponse response){
        channelList = Lists.newArrayList();
        $.setUsername(response.getUsername());
        $.setUpdator(response.getUpdator());
        $.setIscommit(response.getIscommit());
        $.setIsapprove(response.getIsapprove());
        $.setIssuccess(response.getIssuccess());
        $.setIsDistinct(response.getIsDistinct());
        $.setPartnertype(response.getPartnertype());
        $.setPartnername(response.getPartnername());
        $.setCreator(response.getCreatetor());
        $.setCreateTime(response.getCreatetime());
        $.setId(response.getId());
        $.setChannel_n(response.getChannel_n());
        if (StringUtils.isNotBlank(response.getChannel_n())){
            Arrays.stream(response.getChannel_n().split(",")).forEach(arr->channelList.add(arr.trim()));
        };
    }

    /**
     * 数据增加
     */
    public void onCreate(){
        addData();
    }

    /**
     * 数据删除
     * @param id
     */
    @Transactional
    public void onDelete(Long id){
        try {
            service.deleteInfoById(id);
            addSuccessfullyMessage();
            deleteRedis();
        } catch (Exception e) {
            LogService.error("删除数据异常",e);
            addErrorMessage("系统异常,请联系管理员");
        }
    }

    /**
     * 数据修改
     */
    public void onEdit(){
        updateData();
    }

    /**
     * 数据操作
     */
    @Transactional
    public void addData(){
        $.setChannel_n(channelList.toString().replace("[","").replace("]",""));
        LoanLargeAmountPartnerControl controlInfo = new LoanLargeAmountPartnerControl();
        try {
            BeanUtils.copyProperties($,controlInfo);
            service.saveOrUpdate(controlInfo);
            addSuccessfullyMessage();
            deleteRedis();
        } catch (Exception e) {
            LogService.error("日期转换异常",e);
            addErrorMessage("系统异常，请联系管理员");
        }
    }

    /**
     * 更新操作
     */
    @Transactional
    public void updateData(){
        $.setChannel_n(channelList.toString().replace("[","").replace("]",""));
        try {
            $.setCfrom(service.judgeCfrom($.getPartnertype()));
            $.setUpdator(CurrentUser.get().getName());
            $.setUpdateTime(sdf.parse(DateUtils.getCurrentTime()));
            service.updateInfoById($);
            addSuccessfullyMessage();
            deleteRedis();
        } catch (Exception e) {
            LogService.error("日期转换异常",e);
            addErrorMessage("系统异常，请联系管理员");
        }
    }

    /**
     * 重置
     */
    public void onReset(){
        init();
    }

    /**
     * 获取合作方甲方信息
     * @return
     */
    public List<LoanWholeSaleComment> getPartnerInfo(){
        return service.getPartnerInfo();
    }

    /**
     * 删除缓存
     */
    private void  deleteRedis(){
        stringRedisTemplate.delete("loan_large_amount_partner:all");
    }

    /**
     * 查询所有的代理渠道信息
     * @return
     */
    public List<LoanChannelNdInfo> findAllChannelInfo(){
        return service.findAllChannelN();
    }
}
